﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using System.Globalization;
using System.Web.UI.WebControls.WebParts;

namespace XSLTListitemWebPart
{
    public abstract class EditorPartHelper
    {

        public static HtmlGenericControl CreateToolPartToggleSwitch(Control toolPart, string expandToolTip, string collapseToolTip, string displayText, string id, string helpTopic)
        {
            if (id.Contains(" "))
            {
                throw new Exception("ID may not contain spaced");
            }
            //CommonUtilities.ConfirmNotNull(toolPart, "toolPart");
            Table child = new Table();
            child.CellSpacing = 0;
            child.CellPadding = 0;
            child.Style.Add(HtmlTextWriterStyle.Width, "100%");
            toolPart.Controls.Add(child);
            TableRow row = new TableRow();
            child.Controls.Add(row);
            TableCell cell = new TableCell();
            row.Controls.Add(cell);
            HtmlGenericControl control = new HtmlGenericControl("div");
            control.ID = id;
            toolPart.Controls.Add(control);
            HyperLink link = new HyperLink();
            link.ID = id + "_IMAGEANCHOR";
            link.Attributes.Add("onkeydown", "javascript:MSOMenu_KeyboardClick(this, 13, 32);");
            link.Attributes.Add("TabIndex", "0");
            link.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
            Image image = new Image();
            image.ID = id + "_IMAGE";
            HyperLink link2 = new HyperLink();
            link2.Attributes.Add("onkeydown", "MSOMenu_KeyboardClick(this, 13, 32);");
            link2.Attributes.Add("TabIndex", "-1");
            link2.ID = id + "_ANCHOR";
            link2.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
            link2.Text = " &nbsp;" + SPHttpUtility.HtmlEncode(displayText);
            Table table2 = new Table();
            TableRow row2 = new TableRow();
            TableCell cell2 = new TableCell();
            TableCell cell3 = new TableCell();
            table2.Width = Unit.Percentage(100.0);
            table2.CellSpacing = 0;
            table2.CellPadding = 0;
            cell.Controls.Add(table2);
            table2.Controls.Add(row2);
            row2.Controls.Add(cell2);
            row2.Controls.Add(cell3);
            HtmlGenericControl control2 = new HtmlGenericControl("div");
            control2.Attributes.Add("class", "UserSectionTitle");
            cell2.Controls.Add(control2);
            control2.Controls.Add(link);
            link.Controls.Add(new LiteralControl("&nbsp;"));
            link.Controls.Add(image);
            link.Controls.Add(new LiteralControl("&nbsp;"));
            control2.Controls.Add(link2);
            if (!string.IsNullOrEmpty(helpTopic))
            {
                HtmlGenericControl control3 = new HtmlGenericControl("div");
                control3.Attributes.Add("class", "UserSectionTitle");
                cell3.Controls.Add(control3);
                if (string.Compare(SPResource.GetString("AlignRight", new object[0]), "Right", StringComparison.OrdinalIgnoreCase) != 0)
                {
                    cell3.HorizontalAlign = HorizontalAlign.Left;
                }
                else
                {
                    cell3.HorizontalAlign = HorizontalAlign.Right;
                }
                HyperLink link3 = new HyperLink();
                link3.NavigateUrl = "javascript:HelpWindowKey('" + helpTopic + "');";
                link3.Text = SPHttpUtility.NoEncode(/*Resources.GetString(*/"MultiPagesHelpLinkText"/*)*/);
                control3.Controls.Add(link3);
            }
            link.Attributes.Add("name", link.ClientID);
            image.Attributes.Add("name", image.ClientID);
            string script = string.Format(CultureInfo.InvariantCulture, "function mso_onclick{0}() {{ MSOTlPn_ToggleDisplay('{1}', '{2}', '{3}', '{4}', '{5}', '{6}'); }}", new object[] { id, SPHttpUtility.EcmaScriptStringLiteralEncode(control.ClientID), SPHttpUtility.EcmaScriptStringLiteralEncode(image.ClientID), SPHttpUtility.EcmaScriptStringLiteralEncode(link2.ClientID), SPHttpUtility.EcmaScriptStringLiteralEncode(expandToolTip), SPHttpUtility.EcmaScriptStringLiteralEncode(collapseToolTip), SPHttpUtility.EcmaScriptStringLiteralEncode(link.ClientID) });
            toolPart.Page.ClientScript.RegisterClientScriptBlock(typeof(EditorPartHelper), "mso_onclick" + id, script, true);
            link.Attributes.Add("onclick", "javascript:mso_onclick" + id + "();");
            link2.Attributes.Add("onclick", "javascript:mso_onclick" + id + "();");
            link2.Attributes.Add("title", expandToolTip);
            image.Attributes.Add("alt", expandToolTip);
            image.ImageUrl = "/_layouts/images/TPMax2.gif";
            control.Style.Add(HtmlTextWriterStyle.Display, "none");
            return control;
        }

 

 


        public static HtmlGenericControl createConfigureSectionControls(EditorPart toolPart, string expandToolTip, string collapseToolTip, string displayText, string id, string helpTopic,out Table sectionTable )
        {
            HtmlGenericControl configureSection = CreateToolPartToggleSwitch(toolPart, expandToolTip, collapseToolTip, displayText, id, helpTopic );
            sectionTable = new Table();
            sectionTable.CellPadding = 0;
            sectionTable.CellSpacing = 0;
            sectionTable.Style["border-collapse"] = "collapse";
            sectionTable.Attributes.Add("width", "100%");            
            configureSection.Controls.Add(sectionTable);
            return configureSection;
        }

        public static void addToolPaneControl(Table sectionTable, string label, Control newControl, bool bold)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            Label child = new Label();
            child.Text = SPHttpUtility.HtmlEncode(label);
            if (bold) cell.Controls.Add(new LiteralControl("<b>"));
            cell.Controls.Add(child);
            cell.Controls.Add(new LiteralControl(":"));
            if (bold) cell.Controls.Add(new LiteralControl("</b>"));
            cell.Controls.Add(new LiteralControl("</div>"));
            cell.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\"><div class=\"UserControlGroup\"><nobr>"));
            cell.Controls.Add(newControl);
            cell.Controls.Add(new LiteralControl("</nobr></div></div>"));
            cell.Controls.Add(new LiteralControl("<div style='width:100%' class='UserDottedLine'></div>"));
            row.Cells.Add(cell);
            sectionTable.Rows.Add(row);
        }
    }
}
